﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using QLXuongCNTT.Object;
namespace QLXuongCNTT.DataLayers
{
    class dtlPhieunhap
    {
        ConnectDB conn;
        SqlCommand cmd;
        string error;

        public string Error
        {
            get { return error; }
            set { error = value; }
        }
        public dtlPhieunhap()
        {
            conn = new ConnectDB();
            error = "";
        }
        public DataTable getPN_nam()
        {
            error = "";
            string sql = "Select distinct year(NGAY) from Phieunhap";
            cmd = new SqlCommand();
            cmd.CommandText = sql;
            conn.GetData(cmd);
            error = conn.Error;
            return conn;
        }
        public DataTable getPN(int nam)
        {
            error = "";
            string sql = "Select * from Phieunhap where year(ngay)='"+nam+"'";
            cmd = new SqlCommand();
            cmd.CommandText = sql;
            conn.GetData(cmd);
            error = conn.Error;
            return conn;
        }
        public void insertPN(objPhieunhap PN)
        {
            error = "";
            string sql="Insert into Phieunhap values('";
            sql+=PN.Sophieu+"','";
            sql+=PN.Ngaynhap+"','";
            sql+=PN.Nguoinhan+"','";
            sql+=PN.Donvi+"',N'";
            sql+=PN.Lydo+"',N'";
            sql+=PN.Noixuat+"')";
            cmd = new SqlCommand();
            cmd.CommandText = sql;
            int rs = conn.Executive(cmd);
            if (rs <= 0)
                error = conn.Error;
        }
        
        public DataTable Get_PHIEUNHAP(string pn)
        {
            error = "";
            string sql = "select * from PHIEUNHAP where SOPHIEU='" +pn + "'";
            cmd = new SqlCommand();
            cmd.CommandText = sql;
            conn.GetData(cmd);
            error = conn.Error;
            return conn;

        }
        public void DeletePN(string pn)
        {
            error = "";
            string sql = "Delete from PHIEUNHAP where SOPHIEU ='" + pn + "'";
            cmd = new SqlCommand();
            cmd.CommandText = sql;
            conn.Executive(cmd);
            error = conn.Error;
        }
        public void UpdatePN(objPhieunhap PN)
        {
            error = "";
            string sql = "Update Phieunhap set Ngay='";
            sql += PN.Ngaynhap + "',nguoinhan =N'";
            sql += PN.Nguoinhan + "', Donvi =N'";
            sql += PN.Donvi + "',Lydo=N'";
            sql += PN.Lydo + "',Noixuat=N'";
            sql += PN.Noixuat + "' where sophieu='";
            sql += PN.Sophieu + "'";
            cmd = new SqlCommand();
            cmd.CommandText = sql;
            int rs = conn.Executive(cmd);
            if (rs <= 0)
                error = conn.Error;
        }
        
    }
}
